package 集合;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @ClassName : TreeMapText
 * @Author : LV88VL
 * @Date: 2021/3/12 10:15
 * @Description : TreeMap接口
 */
public class TreeMapText {
    public static class ComparatorName1 implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            King k1=(King)o1;
            King k2=(King)o2;
            int temp= k1.getName().compareTo(k2.getName());
            return temp==0?k1.getMoney()- k1.getMoney():temp;
        }
    }
    public static void main(String[] args) {
        //1.。创建Treemap集合对象
        Map<King, String> map = new TreeMap<King, String>(new ComparatorName1());
        //2.。添加元素
        map.put(new King("qianglong", 1000), "北京");
        map.put(new King("kangxi", 2000), "四川");
        map.put(new King("cixi", 3000), "天津");
        map.put(new King("kangxi", 1000), "广州");
        //3.。取出元素 使用entrySet方法
        for(Map.Entry<King,String> me : map.entrySet()){
            King key =me.getKey();
            String value =me.getValue();
            System.out.println(key+"::"+value);
        }
    }

}
